'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 七日登陆奖励
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var moment = require('moment');
var Code = require('../../../consts/code');
var moduleType = require('../../../consts/moduleType');
var operationType = require('../../../consts/operationType');
var PlayerManager = require('../../../libs/playerManager');
var activityConf = require('../../../config/activityConf');
var rewardService = require('../../../services/rewardService');
var dataApi = require('../../../libs/dataApi');
var activityService = require('../../../services/activityService');
var actType = require('../../../consts/activityType.js');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 查询“七日登陆”活动列表
 */
Handler.prototype.list = function (msg, session, next) {
    var role_id = session.uid;

    var total_login_days;
    var receieved_day_list = [];

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['role', 'activity', 'accrued'], cb);
            },
            function (cb) {
                var role_model = player_manager.role;
                var activity_model = player_manager.activity;

                total_login_days = activity_model.continuously_login_days;
                receieved_day_list = activity_model.seven_login_received_day_list;

                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    total_login_days: total_login_days,
                    receieved_day_list: receieved_day_list
                }
            });

        }
    );
};

/**
 * 领取“七日登陆”奖励
 */
Handler.prototype.reward = function (msg, session, next) {
    var role_id = session.uid;
    var reward_day = msg.reward_day;

    if (!reward_day) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var reward_list = [];
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'activity', 'arena', 'expedition', 'bag', 'build', 'soldier', 'task', 'achieve', 'daily_task', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var role_model = player_manager.role;
                var activity_model = player_manager.activity;

                // 是否已经到达可以领取天数
                if (reward_day > activity_model.getContinuouslyLoginDays()) {
                    return next(null, {
                        code: Code.ACTIVITY_NOT_REACH_CAN_RECEIVE_DAY
                    });
                }

                // 是否重复领取
                if (!activity_model.addSevenLoginReceivedDays(reward_day)) {
                    return next(null, {
                        code: Code.ACTIVITY_HAVE_RECEIVE_BEFORE
                    });
                }

                reward_list = activityConf.getSevenLoginRewardList(reward_day, activity_model.first_week);
                if (!reward_list) {
                    return next(null, {
                        code: Code.REWARD_ERROR
                    });
                }
                rewardService.addRewardListToRole(player_manager, reward_list, moduleType.MDL_ACT_SEVEN_LOGIN, operationType.OPR_ACT_SEVEN_LOGIN);
                player_manager.save(cb);
                activityService.push(role_id, actType.SEVEN_LOGIN, player_manager);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    reward_list: reward_list
                }
            });
        }
    );
};
